package com.azkj.album.view.widgets;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.azkj.album.R;
import com.azkj.album.network.utils.ToastUtils;
import com.azkj.album.utils.SelectDateUtil;

import java.text.SimpleDateFormat;
import java.util.Date;


public class ChooseDateView extends LinearLayout {
    private Context mContext;
    private TextView tvStart, tvEnd, tvReset, tvSearch;

    private String start, end;
    private boolean isCheckDate = true;
    private View root;
    private OnSearchListener mSearchClickListener;


    public ChooseDateView(Context context) {
        super(context);
        initView(context);
    }


    public ChooseDateView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    private void initView(Context context) {
        this.mContext = context;
        root = View.inflate(context, R.layout.layout_choose_date, this);
        tvStart = root.findViewById(R.id.tv_start_time);
        tvEnd = root.findViewById(R.id.tv_end_time);
        tvReset = root.findViewById(R.id.tv_reset);
        tvSearch = root.findViewById(R.id.tv_search);

        tvReset.setOnClickListener(v -> {
            tvStart.setText("");
            tvEnd.setText("");
            start = null;
            end = null;
            if (mSearchClickListener != null) {
                mSearchClickListener.onReset();
            }
        });
        tvSearch.setOnClickListener(v -> {
            if (isCheckDate && (TextUtils.isEmpty(start) || TextUtils.isEmpty(end))) {
                ToastUtils.showCenterToast("请选择时间");
                return;
            }
            if (mSearchClickListener != null) {
                mSearchClickListener.onSearchClick(start, end);
            }
        });
        tvStart.setOnClickListener(v -> SelectDateUtil.showDatePickDialog(mContext, (view1, year, month, dayOfMonth) -> {
            String old = start;
            start = String.format("%d-%d-%d", year, month + 1, dayOfMonth);
            if (checkStartAndEndDate()) {
                start = old;
                return;
            }

            tvStart.setText(start);


        }, start));
        tvEnd.setOnClickListener(v -> SelectDateUtil.showDatePickDialog(mContext, (view1, year, month, dayOfMonth) -> {
            String old = end;
            end = String.format("%d-%d-%d", year, month + 1, dayOfMonth);
            if (checkStartAndEndDate()) {
                end = old;
                return;
            }

            tvEnd.setText(end);


        }, end));


    }

    public void setHint(String start, String end) {
        this.start = start;
        this.end = end;
        tvStart.setText(start);
        tvEnd.setText(end);
    }

    private boolean checkStartAndEndDate() {


        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");


        try {
            Date date1 = format.parse(start);
            Date date2 = format.parse(end);
            if (date1.after(date2)) {
                ToastUtils.showCenterToast("开始日期不能晚于结束日期");
                return true;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }


        return false;
    }


    public void setOnSearchClickListener(OnSearchListener listener) {
        this.mSearchClickListener = listener;
    }

    public void isCheckDate(boolean check) {
        this.isCheckDate = check;
    }

    public interface OnSearchListener {
        void onSearchClick(String start, String end);

        void onReset();

    }


}
